home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / QuickDraw3D 1.6 SDK / Mac SampleCode New for 1.6 / CullGroupSample / Source / 3DMF.c next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  8.4 KB  |  386 lines  |  [TEXT/MPCC]

  1. /****************************/
  2. /*       3DMF.C                    */
  3. /* By Brian Greenstone      */
  4. /****************************/
  5.  
  6.  
  7. /****************************/
  8. /*    EXTERNALS             */
  9. /****************************/
  10. #include <Script.h>
  11.  
  12. #include <QD3D.h>
  13. #include <QD3DGroup.h>
  14. #include <QD3DStorage.h>
  15. #include <QD3DIO.h>
  16. #include <QD3DErrors.h>
  17. #include <QD3DGeometry.h>
  18. #include "myglobals.h"
  19. #include "misc.h"
  20. #include "3dmf.h"
  21. #include "mywindows.h"
  22. #include "process.h"
  23.  
  24. extern    WindowPtr    gBreakdownWindow;
  25. extern    Boolean        gShowContentStructure,gSaveTextToFile;
  26.  
  27.  
  28. /****************************/
  29. /*    PROTOTYPES            */
  30. /****************************/
  31.  
  32. static TQ3Status MyRead3DMFModel(TQ3FileObject file, TQ3Object *model, TQ3Object *viewHints);
  33. static Boolean Get3DMFInputFileName(FSSpec *myFSSpec);
  34. static TQ3FileObject    Create3DMFFileObject(FSSpec *myFSSpec);
  35. static Boolean Get3DMFOutputFileName(FSSpec *myFSSpec);
  36.  
  37.  
  38.  
  39. /****************************/
  40. /*    CONSTANTS             */
  41. /****************************/
  42.  
  43.  
  44. /*********************/
  45. /*    VARIABLES      */
  46. /*********************/
  47.  
  48. FSSpec    gLast3DMF_FSSpec;
  49.  
  50.  
  51. /*************** LOAD 3DMF MODEL **************/
  52. //
  53. // INPUT: inFile = nil if use Standard File Dialog, else FSSpec of file to load
  54. //
  55. // OUTPUT: nil = didnt happen
  56. //
  57.  
  58. TQ3Object    Load3DMFModel(FSSpec *inFile)
  59. {
  60. TQ3FileObject        fileObj;
  61. TQ3Object            theModel,viewHints;
  62. FSSpec                myFSSpec;
  63.  
  64.  
  65.     if (inFile == nil)                                        // see if need to let user choose file
  66.     {
  67.             /* SELECT FILE TO OPEN */
  68.             
  69.         if (!Get3DMFInputFileName(&myFSSpec))
  70.             return(nil);
  71.     }
  72.     else
  73.         myFSSpec = *inFile;                                    // use input FSSpec
  74.         
  75.     gLast3DMF_FSSpec = myFSSpec;                            // also keep a global copy
  76.  
  77.             /* CREATE A FILE OBJECT */
  78.                 
  79.     fileObj = Create3DMFFileObject(&myFSSpec);
  80.     if (fileObj == nil)
  81.         theModel = nil;
  82.     else
  83.     {
  84.  
  85.             /* READ THE 3DMF FILE */
  86.             
  87.         if (MyRead3DMFModel(fileObj, &theModel, &viewHints) == kQ3Failure)
  88.             theModel = nil;
  89.     }
  90.     
  91.     Q3Object_Dispose(fileObj);
  92.  
  93.  
  94.     return(theModel);
  95. }
  96.  
  97.  
  98. /*************** CREATE 3DMF FILE OBJECT ****************/
  99. //
  100. // Creates and returns a File Object which contains the 3DMF file.
  101. //
  102. // INPUT: myFSSpec = file to create object for
  103. //
  104.  
  105. static TQ3FileObject    Create3DMFFileObject(FSSpec *myFSSpec)
  106. {
  107. TQ3FileObject        myFileObj;
  108. TQ3StorageObject    myStorageObj;
  109.         
  110.         /* CREATE NEW STORAGE OBJECT WHICH IS THE 3DMF FILE */
  111.             
  112.     myStorageObj = Q3FSSpecStorage_New(myFSSpec);
  113.     if (myStorageObj == nil)
  114.     {
  115.         DoFatalAlert("\pError calling Q3FSSpecStorage_New");
  116.         return(nil);
  117.     }
  118.     
  119.     
  120.             /* CREATE NEW FILE OBJECT */
  121.             
  122.     myFileObj = Q3File_New();
  123.     if (myFileObj == nil)
  124.     {
  125.         DoFatalAlert("\pError calling Q3File_New");
  126.         Q3Object_Dispose(myStorageObj);
  127.         return(nil);
  128.     }
  129.     
  130.             /* SET THE STORAGE FOR THE FILE OBJECT */
  131.             
  132.     if (Q3File_SetStorage(myFileObj, myStorageObj) != kQ3Success)
  133.     {
  134.         DoAlert("\pError Q3File_SetStorage");
  135.         return(nil);
  136.     }
  137.     Q3Object_Dispose(myStorageObj);
  138.             
  139.     return(myFileObj);
  140. }
  141.  
  142.  
  143. /******************* GET 3DMF INPUT FILE NAME ********************/
  144. //
  145. // Does Standard File IO and returns a filled in FSSpec record for selected 3DMF file.
  146. //
  147. // OUTPUT: false = error occurred
  148. //            FSSpec = file info
  149. //
  150.  
  151. static Boolean Get3DMFInputFileName(FSSpec *myFSSpec)
  152. {
  153. StandardFileReply    reply;
  154. SFTypeList    typeList;
  155. short        numTypes;
  156.  
  157.     typeList[0] = '3DMF';
  158.     typeList[1] = 'TEXT';
  159.     numTypes = 2;
  160.         
  161.     StandardGetFile(nil,numTypes,typeList,&reply);
  162.     if (!reply.sfGood)                                // see if cancelled 
  163.         return(false);
  164.  
  165.     *myFSSpec = reply.sfFile;                        // copy FSSpec back
  166.     return(true);
  167. }
  168.  
  169.  
  170.  
  171. /***************** READ 3DMF MODEL ************************/
  172. //
  173. // Reads the 3DMF file and saves into new TQ3Object.
  174. //
  175. // INPUT:  file = File Object containing reference to 3DMF file
  176. //
  177. // OUTPUT: model =object containing all the important data in the 3DMF file
  178. //           viewHints = object containing all viewHints objects???
  179. //
  180. //
  181.  
  182. static TQ3Status MyRead3DMFModel(TQ3FileObject file, TQ3Object *model, TQ3Object *viewHints)
  183. {
  184. TQ3GroupObject    myGroup;
  185. TQ3Object        myObject;
  186.  
  187.         /* INIT VIEW HINTS & MODEL TO BE RETURNED */
  188.         
  189.     *viewHints = *model = nil;                                            // assume return nothing
  190.     myGroup = myObject = nil;
  191.     
  192.         /* OPEN THE FILE OBJECT & EXIT GRACEFULLY IF UNSUCCESSFUL */
  193.  
  194.     if (Q3File_OpenRead(file,nil) != kQ3Success)                            // open the file
  195.     {
  196.         DoFatalAlert("\pReading 3DMF file failed!");
  197.         return    kQ3Failure;
  198.     }
  199.     
  200.     do
  201.     {
  202.         myObject = nil;
  203.         
  204.             /* READ A METAFILE OBJECT FROM THE FILE OBJECT */
  205.  
  206.         myObject = Q3File_ReadObject(file);
  207.         if (myObject == nil)
  208.         {
  209.         
  210.             if (myGroup)
  211.                 Q3Object_Dispose(myGroup);
  212.  
  213.             QD3D_ShowError("\pReading returned NULL object!", true);
  214.             break;
  215.         }
  216.                         
  217.         /* SAVE A VIEW HINTS OBJECT & ADD ANY DRAWABLE OBJECTS TO A GROUP */
  218.         
  219.         if (Q3Object_IsType(myObject, kQ3SharedTypeViewHints))            // see if is a View Hint object
  220.         {
  221.             if (*viewHints == nil)
  222.             {
  223.                 *viewHints = myObject;
  224.                 myObject = NULL;
  225.             }
  226.         }
  227.         else
  228.         if (Q3Object_IsDrawable(myObject))                                // see if is a drawable object
  229.         {
  230.             if (myGroup)                                                // if group exists
  231.             {
  232.                 Q3Group_AddObject(myGroup,myObject);                    // add object to group
  233.             }
  234.             else
  235.             if (*model == nil)                                            // if no model data yet
  236.             {
  237.                 *model = myObject;                                        // set model to this object
  238.                 myObject = nil;                                            // clear this object
  239.             }
  240.             else
  241.             {
  242.                 myGroup = Q3DisplayGroup_New();                            // make new group
  243.                 Q3Group_AddObject(myGroup,*model);                        // add existing model to group
  244.                 Q3Group_AddObject(myGroup,myObject);                    // add object to group
  245.                 Q3Object_Dispose(*model);                                // dispose model
  246.                 *model = myGroup;                                        // set return value to the new group
  247.             }
  248.         }
  249.         
  250.         if (myObject != nil)
  251.             Q3Object_Dispose(myObject);
  252.     } while(!Q3File_IsEndOfFile(file));
  253.     
  254.     if (Q3Error_Get(nil) != kQ3ErrorNone)
  255.     {
  256.         if (*model != nil)
  257.         {
  258.             Q3Object_Dispose(*model);
  259.             *model = nil;
  260.         }
  261.         
  262.         if (*viewHints != nil)
  263.         {
  264.             Q3Object_Dispose(*viewHints);
  265.             *viewHints = nil;
  266.         }
  267.         return(kQ3Failure);
  268.     }
  269.     return(kQ3Success);
  270. }
  271.     
  272.  
  273. /*************** SAVE 3DMF MODEL **************/
  274. //
  275. // INPUT: outFile = nil if use Standard File Dialog, else FSSpec of file to load
  276. //
  277. //
  278.  
  279. void Save3DMFModel(QD3DSetupOutputType *setupInfo,FSSpec *outFile, void (*callBack)(QD3DSetupOutputType *))
  280. {
  281. TQ3FileObject        fileObj;
  282. TQ3Object            theModel;
  283. FSSpec                myFSSpec;
  284. TQ3Status                myStatus;
  285. TQ3ViewStatus            myViewStatus;
  286. TQ3FileMode            fileMode;
  287.  
  288. #if DEMO_VERSION
  289.     DoAlert("\pSorry, the demo version of 3DMF Optimizer cannot save files, but the optimized model will now be displayed in the Model Window.");
  290. #else
  291.  
  292.     if (outFile == nil)                                        // see if need to let user choose file
  293.     {
  294.             /* SELECT FILE TO OPEN */
  295.             
  296.         if (!Get3DMFOutputFileName(&myFSSpec))
  297.             return;
  298.     }
  299.     else
  300.     {
  301.         myFSSpec = *outFile;                                // use input FSSpec    
  302.         FSpDelete(&myFSSpec);                                // delete any old one
  303.         FSpCreate(&myFSSpec, 'OP20', '3DMF', smSystemScript);        // create new file
  304.     }
  305.     
  306.     gLast3DMF_FSSpec = myFSSpec;                            // also keep a global copy
  307.  
  308.             /* CREATE A FILE OBJECT */
  309.                 
  310.     fileObj = Create3DMFFileObject(&myFSSpec);
  311.     if (fileObj == nil)
  312.         theModel = nil;
  313.     else
  314.     {
  315.                                             // set binary or text output
  316. //        fileMode = kQ3FileModeNormal;
  317.         fileMode = kQ3FileModeText;
  318.             
  319.         myStatus = Q3File_OpenWrite(fileObj, fileMode);
  320.         if ( myStatus != kQ3Success )
  321.             DoFatalAlert("\pQ3File_OpenWrite Failed!");
  322.     
  323.             /* WRITE THE 3DMF FILE */
  324.             
  325.                 
  326.         myStatus = Q3View_StartWriting(setupInfo->viewObject,fileObj);            
  327.         if ( myStatus != kQ3Success )
  328.             DoFatalAlert("\pQ3View_StartWriting Failed!");
  329.         
  330.                 /***************/
  331.                 /* SUBMIT LOOP */
  332.                 /***************/
  333.         do
  334.         {
  335.     
  336.                 /* CALL INPUT WRITE FUNCTION */
  337.     
  338.             callBack(setupInfo);
  339.     
  340.             myViewStatus = Q3View_EndWriting(setupInfo->viewObject);
  341.         } while ( myViewStatus == kQ3ViewStatusRetraverse );            
  342.     }
  343.     
  344.     myStatus = Q3File_Close(fileObj);
  345.     if ( myStatus == kQ3Failure )
  346.         DoFatalAlert("\pQ3File_Close Failed!");
  347.  
  348.     Q3Object_Dispose(fileObj);
  349.  
  350. #endif
  351. }
  352.     
  353.     
  354.     
  355. /******************* GET 3DMF OUTPUT FILE NAME ********************/
  356. //
  357. // Does Standard File IO and returns a filled in FSSpec record for selected 3DMF file & creates the file!
  358. //
  359. // OUTPUT: false = error occurred
  360. //            FSSpec = file info
  361. //
  362.  
  363. static Boolean Get3DMFOutputFileName(FSSpec *myFSSpec)
  364. {
  365. StandardFileReply    reply;
  366.     
  367.         
  368.     StandardPutFile("\pSave 3DMF file",gLast3DMF_FSSpec.name,&reply);
  369.     if (!reply.sfGood)                                // see if cancelled 
  370.         return(false);
  371.  
  372.     *myFSSpec = reply.sfFile;                        // copy FSSpec back
  373.     
  374.     if (reply.sfReplacing)
  375.         FSpDelete(myFSSpec);                                    // delete any old one
  376.     FSpCreate(myFSSpec, 'OP20', '3DMF', smSystemScript);        // create new file
  377.     
  378.     return(true);
  379. }
  380.  
  381.     
  382.     
  383.     
  384.     
  385.     
  386.